package com.shensi.adminCollect.controller.station;

import com.shensi.common.core.controller.BaseController;
import com.shensi.common.core.domain.AjaxResult;
import com.shensi.common.core.domain.entity.SysDept;
import com.shensi.common.core.page.TableDataInfo;
import com.shensi.adminCollect.common.utils.DateUtils;
import com.shensi.adminCollect.common.utils.poi.ExcelUtil;
import com.shensi.adminCollect.domain.station.StationBalanceChange;
import com.shensi.adminCollect.service.station.IStationBalanceChangeService;
import com.shensi.adminCollect.service.system.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

import com.shensi.common.core.domain.model.LoginUser;
import com.shensi.common.core.utils.SecurityUtils;

/**
 * LNG油站余额变化记录Controller
 *
 * @author qujia
 * @date 2021-07-13
 */
@RestController
@RequestMapping("/station/stationBalanceChange")
public class StationBalanceChangeController extends BaseController
{
    @Autowired
    private IStationBalanceChangeService stationBalanceChangeService;
    @Autowired
    private ISysDeptService deptService;
    /**
     * 查询LNG油站余额变化记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(StationBalanceChange stationBalanceChange)
    {
        List<String> list = deptService.levelDeptList(stationBalanceChange.getLevelId());
        if(list!=null && list.size()>0){
            stationBalanceChange.setStationIdList(list);
            stationBalanceChange.setStationId(null);
        }
        startPage();
        List<StationBalanceChange> changeList = stationBalanceChangeService.selectStationBalanceChangeList(stationBalanceChange);
        return getDataTable(changeList);
    }

    /**
     * 导出LNG油站余额变化记录列表
     */
    @GetMapping("/export")
    public AjaxResult export(StationBalanceChange stationBalanceChange)
    {
        List<String> list = deptService.levelDeptList(stationBalanceChange.getLevelId());
        if(list!=null && list.size()>0){
            stationBalanceChange.setStationIdList(list);
            stationBalanceChange.setStationId(null);
        }
        List<StationBalanceChange> changeList = stationBalanceChangeService.selectStationBalanceChangeList(stationBalanceChange);
        ExcelUtil<StationBalanceChange> util = new ExcelUtil<StationBalanceChange>(StationBalanceChange.class);
        return util.exportExcel(changeList, "change");
    }

    /**
     * 获取LNG油站余额变化记录详细信息
     */
     @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(stationBalanceChangeService.selectStationBalanceChangeById(id));
    }

    /**
     * 新增LNG油站余额变化记录
     */
    @PostMapping("/add")
    @Transactional
    public AjaxResult add(@RequestBody StationBalanceChange stationBalanceChange)
    {
        int i=0;
        //获取当前站上余额是多少
        LoginUser currentUser = SecurityUtils.getLoginUser();
        if(stationBalanceChange.getStationId()!=null && stationBalanceChange.getAmt()!=null && stationBalanceChange.getType()!=null){
            SysDept dept =deptService.selectDeptById(stationBalanceChange.getStationId());
            stationBalanceChange.setNowAmt(dept.getBalance());
            if(dept!=null){
                if( dept.getBalance()!=null){
                    if(stationBalanceChange.getType().equals("+")){
                        dept.setBalance(dept.getBalance().add(stationBalanceChange.getAmt()));
                    }else if(stationBalanceChange.getType().equals("-")){
                        dept.setBalance(dept.getBalance().subtract(stationBalanceChange.getAmt()));
                    }
                }else  {
                    if(stationBalanceChange.getType().equals("+")){
                        dept.setBalance(stationBalanceChange.getAmt());
                    }else if(stationBalanceChange.getType().equals("-")){
                        dept.setBalance(new BigDecimal("0").subtract(stationBalanceChange.getAmt()));
                    }
                }
                i = deptService.updateDeptInfo(dept);
                stationBalanceChange.setCreateBy(currentUser.getUser().getUserId()+"");
                stationBalanceChange.setCreateTime(DateUtils.getNowDate());
                i = stationBalanceChangeService.insertStationBalanceChange(stationBalanceChange);
            }else{
                logger.debug("没有查询到站点信息");
            }
        }
        return toAjax(i);
    }

    /**
     * 修改LNG油站余额变化记录
     */
    @PutMapping
    public AjaxResult edit(@RequestBody StationBalanceChange stationBalanceChange)
    {
        return toAjax(stationBalanceChangeService.updateStationBalanceChange(stationBalanceChange));
    }

    /**
     * 删除LNG油站余额变化记录
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(stationBalanceChangeService.deleteStationBalanceChangeByIds(ids));
    }
}
